//
//  RootCVCOfVideo.m
//  To Feel
//
//  Created by 曹宇 on 16/7/14.
//  Copyright © 2016年 caoyu. All rights reserved.
//

#import "RootCVCOfVideo.h"
#import "CellOfRootCVCVideo.h"
#import "NetworkingHandler.h"
#import "ModelOfFirstCVC.h"
#import "RootVCOfSencondTVC.h"
@interface RootCVCOfVideo ()
@property(nonatomic, retain) NSMutableArray *arrOfModel;

@end

@implementation RootCVCOfVideo
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"视频";
    [self configCollectionView];
    [self handleData];
}

- (void)configCollectionView {
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.collectionView registerClass:[CellOfRootCVCVideo class] forCellWithReuseIdentifier:@"CellOfRootCVCVideo"];
}

- (void)handleData {
    
    self.arrOfModel = [NSMutableArray array];
    
    NSString *str = @"http://baobab.wandoujia.com/api/v3/discovery?_s=b3006581929919aba88ea89642dceba1&f=iphone&net=wifi&u=b3dd1f64e4b4e6660ab603a76e310e3e117917af&v=2.2.0&vc=721";
    
    NetworkingHandler *nt = [[NetworkingHandler alloc] init];
    [nt netWorkingHandlerGETWithURL:str completion:^(id result, NSData *data, NSURLResponse *response, NSError *error) {
        
        NSArray *arr = [result objectForKey:@"itemList"];
        for (NSDictionary *dic in arr) {
            if ([dic[@"type"] isEqualToString:@"squareCard"]) {
                ModelOfFirstCVC *model = [[ModelOfFirstCVC alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [self.arrOfModel addObject:model];
            }
        }
        [self.arrOfModel removeObjectAtIndex:0];
        [self.arrOfModel removeLastObject];
        
        [self.collectionView reloadData];
        
    }];
    
}

#pragma mark <UICollectionViewDataSource>



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _arrOfModel.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CellOfRootCVCVideo *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellOfRootCVCVideo" forIndexPath:indexPath];
    
    cell.model = [_arrOfModel objectAtIndex:indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ModelOfFirstCVC *model = [_arrOfModel objectAtIndex:indexPath.row];
    
    RootVCOfSencondTVC *vc = [[RootVCOfSencondTVC alloc] initWithIdOfPage:[[model.data objectForKey:@"id"] stringValue]];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    
}



@end
